﻿using MediatR;
using Warehouse.Api.Write.AllCation.Command.Breakage;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Write.AllCation.Hanlder
{
    public class DeleteBreakageCommandHanlder : IRequestHandler<DeleteBreakageCommand, ApiResult<int>>
    {
        public readonly IBaseRepostity<Breakage> _breakageRepostity;
        public readonly ILogger<DeleteBreakageCommandHanlder> logger;

        public DeleteBreakageCommandHanlder(IBaseRepostity<Breakage> breakageRepostity, ILogger<DeleteBreakageCommandHanlder> logger)
        {
            _breakageRepostity = breakageRepostity;
            this.logger = logger;
        }
        /// <summary>
        /// 报损单删除的接口
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<ApiResult<int>> Handle(DeleteBreakageCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var res = _breakageRepostity.GetAll().Where(x => x.Id == request.Id).First();
                res.IsDeleted = true;
                int r = _breakageRepostity.UpdateAsync(res).Result;
                if (r > 0)
                {
                    result.Msg = "删除成功";
                    logger.LogInformation("删除成功");
                    result.Data = r;
                    result.Code = ApiEume.Success;
                }
                else
                {
                    result.Msg = "删除失败";
                    logger.LogInformation("删除失败");
                    result.Data = r;
                    result.Code = ApiEume.Error;
                }
            }
            catch (Exception ex)
            {

            }
            return Task.FromResult(result);
        }
    }
}
